package com.itcms.webservice.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.util.StringUtils;

import com.itcms.common.orm.LimitRequest;
import com.itcms.common.orm.Limitable;
import com.itcms.common.util.DateUtil;
import com.itcms.common.util.JacksonMapper;
import com.itcms.common.util.ReturnObj;
import com.itcms.core.domain.AttachmentRef;
import com.itcms.core.domain.Info;
import com.itcms.core.domain.Node;
import com.itcms.core.domain.Site;
import com.itcms.core.domain.User;
import com.itcms.core.service.AttachmentRefService;
import com.itcms.core.service.InfoQueryService;
import com.itcms.core.service.InfoService;
import com.itcms.core.service.NodeQueryService;
import com.itcms.core.service.OperationLogService;
import com.itcms.core.service.OrgService;
import com.itcms.core.service.SiteService;
import com.itcms.core.service.UserService;
import com.itcms.ext.domain.EntryInfo;
import com.itcms.ext.domain.Schedule;
import com.itcms.ext.domain.Station;
import com.itcms.ext.domain.TicketPrice;
import com.itcms.ext.service.EntryInfoService;
import com.itcms.ext.service.ScheduleService;
import com.itcms.ext.service.StationService;
import com.itcms.ext.service.TicketPriceService;
import com.itcms.webservice.AppInfoService;
import com.itcms.webservice.domain.NodeVo;
import com.itcms.webservice.domain.SiteVO;

@WebService(endpointInterface="com.itcms.webservice.AppInfoService",serviceName="appInfoService")
public class AppInfoServiceImp implements AppInfoService {
	private static final Logger logger = LoggerFactory
			.getLogger(AppInfoServiceImp.class);

	public static final int INCLUDE_CHILDREN = 0;
	public static final int INCLUDE_MULTI = 1;
	public static final int MAIN_NODE = 2;
	
	public String getInfo(String pwd, String name) {
		if(!pwd.equals("gdjt2010")){
			ReturnObj<String> returnObj =  new ReturnObj("信息密码错误！");
			return JacksonMapper.toJson(returnObj);
		}else if(StringUtils.isEmpty(name) || name.equals("?")){
			ReturnObj<String> returnObj =  new ReturnObj("名称不正确！");
			return JacksonMapper.toJson(returnObj);
		}else{
			try {
				ReturnObj<List> returnObj = new ReturnObj<List>(list(name));
				return JacksonMapper.toJson(returnObj);
			} catch (Exception e) {
				e.printStackTrace();
				ReturnObj<List<NodeVo>> returnObj = new ReturnObj<List<NodeVo>>(e);
				return JacksonMapper.toJson(returnObj);
			}
		}
	}
	
	public List<?> list(String queryNodeName){
		List<Info> infos = new ArrayList<>();
		List list = new ArrayList();
		String queryStatus = null;
		Integer queryInfoPermType=null;
		Integer queryNodeType=null;
		queryNodeType = queryNodeType == null ? 0 : queryNodeType;
		Integer siteId = 5;
		User user = userService.get(1);
		Integer nodeId = null;
		Integer queryNodeId = null;
		Integer mainNodeId = null;
		String treeNumber = null;
		Node queryNode = null;
		if (queryNodeId != null) {
			queryNode = nodeQuery.get(queryNodeId);
		}
		if (queryNodeName != null) {
			queryNode = nodeQuery.findByNumber(siteId, queryNodeName);
			if(queryNode==null){
				return infos;
			}else{
				queryNodeId=queryNode.getId();
			}
		}
		if (queryNode != null) {
			if (queryNodeType == INCLUDE_MULTI) {
				nodeId = queryNodeId;
			} else if (queryNodeType == MAIN_NODE) {
				mainNodeId = queryNodeId;
			} else {
				treeNumber = queryNode.getTreeNumber();
			}
		}
		Map<String, String[]> params = null;
		int infoPermType = user.getInfoPermType(siteId);
		if (queryInfoPermType != null && queryInfoPermType > infoPermType) {
			infoPermType = queryInfoPermType;
		}
		boolean allInfoPerm = user.getAllInfoPerm(siteId);
		Sort sort = new Sort(Direction.DESC, "publishDate", "id");
		Limitable limit = LimitRequest.defaultSort(null, sort);
		infos = query.findAll(siteId, mainNodeId, nodeId,
				treeNumber, user.getId(), allInfoPerm, infoPermType,
				queryStatus, params, limit);
		
		for (Info info : infos) {
//			InfoVO wsInfo = new InfoVO();
			com.itcms.webservice.domain.Info wsInfo = new com.itcms.webservice.domain.Info();
			wsInfo.setContent(info.getClobs().get("text"));
			wsInfo.setCateID(info.getNode().getId().toString());
			wsInfo.setSiteID(siteId.toString());
			wsInfo.setTitle(info.getDetail().getTitle());
			wsInfo.setSubTitle(info.getSubtitle());
			wsInfo.setUrl(info.getDetail().getLink());
			String publishDate = DateUtil.getDateStr(info.getPublishDate(), DateUtil.TIME_FORMAT);
			wsInfo.setPublishedTime(publishDate);
			wsInfo.setWriteDate(publishDate);
			wsInfo.setExpDate(publishDate);
			wsInfo.setLastUpdateDate(publishDate);
			wsInfo.setSource(info.getDetail().getMetaDescription());
			wsInfo.setLastUpdateUserName("");
			wsInfo.setLastUpdateUserId("");
			wsInfo.setWriter(user.getId().toString());
			wsInfo.setWriterName(user.getUsername());
			wsInfo.setTemplateId(info.getTemplate());
			wsInfo.setType(info.getStatus());
			
			List<AttachmentRef> attachmentRefs = attachmentRefService.findByFtypeAndFid("info", info.getId());
			List<Map<String, String>> attachList = new ArrayList<Map<String, String>>();
			for (AttachmentRef attachmentRef : attachmentRefs) {
				Map<String, String> map = new HashMap<String, String>();
				
				StringBuffer name = new StringBuffer();
				name.append(attachmentRef.getAttachment().getName());
				name = name.insert(0, "172.16.7.81:8080/itcms/uploads/");
				map.put("FileName", attachmentRef.getAttachment().getRealName());
				map.put("Url", name.toString());
				attachList.add(map);
				String fileJson = JacksonMapper.toJson(attachList);
				wsInfo.setFilesJsoin(fileJson);
			}
			
			list.add(wsInfo);
		}
		return list;
	}
	
	public String getTicketPrice(String pwd) {
		if(!pwd.equals("gdjt2010")){
			ReturnObj returnObj =  new ReturnObj("信息密码错误！");
			return JacksonMapper.toJson(returnObj);
		}else{
			try {
				List<TicketPrice> ticketPrices = ticketPriceService.findAll();
				ReturnObj<List<TicketPrice>> returnObj = new ReturnObj<List<TicketPrice>>(ticketPrices);
				return JacksonMapper.toJson(returnObj);
			} catch (Exception e) {
				e.printStackTrace();
				ReturnObj<List<TicketPrice>> returnObj = new ReturnObj<List<TicketPrice>>(e);
				return JacksonMapper.toJson(returnObj);
			}
		}
	}
	
	public String getEntryInfo(String pwd, Integer stationId) {
		if(!pwd.equals("gdjt2010")){
			ReturnObj returnObj =  new ReturnObj("信息密码错误！");
			return JacksonMapper.toJson(returnObj);
		}else if(StringUtils.isEmpty(stationId) || stationId.toString().equals("?")){
			ReturnObj<String> returnObj =  new ReturnObj("请输入正确的站点id！");
			return JacksonMapper.toJson(returnObj);
		}else{
			try {
				List<EntryInfo> entryInfos = entryInfoService.findAll(stationId);
				ReturnObj<List<EntryInfo>> returnObj = new ReturnObj<List<EntryInfo>>(entryInfos);
				return JacksonMapper.toJson(returnObj);
			} catch (Exception e) {
				e.printStackTrace();
				ReturnObj<List<EntryInfo>> returnObj = new ReturnObj<List<EntryInfo>>(e);
				return JacksonMapper.toJson(returnObj);
			}
		}
	}
	
	public String getSchedule(String pwd){
		if(!pwd.equals("gdjt2010")){
			ReturnObj returnObj =  new ReturnObj("信息密码错误！");
			return JacksonMapper.toJson(returnObj);
		}else{
			try {
				List<Schedule> schedules = scheduleService.findAll();
				ReturnObj<List<Schedule>> returnObj = new ReturnObj<List<Schedule>>(schedules);
				return JacksonMapper.toJson(returnObj);
			} catch (Exception e) {
				e.printStackTrace();
				ReturnObj<List<Schedule>> returnObj = new ReturnObj<List<Schedule>>(e);
				return JacksonMapper.toJson(returnObj);
			}
		}
	}
	
	public String getStation(String pwd, Integer lineId){
		if(!pwd.equals("gdjt2010")){
			ReturnObj returnObj =  new ReturnObj("信息密码错误！");
			return JacksonMapper.toJson(returnObj);
		}else{
			try {
				List<Station> stations = stationService.findAll(lineId);
				ReturnObj<List<Station>> returnObj = new ReturnObj<List<Station>>(stations);
				return JacksonMapper.toJson(returnObj);
			} catch (Exception e) {
				e.printStackTrace();
				ReturnObj returnObj = new ReturnObj(e);
				return JacksonMapper.toJson(returnObj);
			}
		}
	}
	
	public String getSite(String pwd){
		if(!pwd.equals("gdjt2010")){
			ReturnObj<Object> returnObj =  new ReturnObj<Object>("信息密码错误！");
			return JacksonMapper.toJson(returnObj);
		}else{
			try {
				Site site = siteService.get(5);
				SiteVO siteVO = new SiteVO();
				siteVO.setId(site.getId());
				siteVO.setName(site.getName());
				siteVO.setDomain(site.getDomain());
				ReturnObj<SiteVO> returnObj = new ReturnObj<SiteVO>(siteVO);
				return JacksonMapper.toJson(returnObj);
			} catch (Exception e) {
				e.printStackTrace();
				ReturnObj<Object> returnObj = new ReturnObj<Object>(e);
				return JacksonMapper.toJson(returnObj);
			}
		}
	}
	
	@Autowired
	private EntryInfoService entryInfoService;
	@Autowired
	private TicketPriceService ticketPriceService;
	@Autowired
	private ScheduleService scheduleService;
	@Autowired
	private StationService stationService;
	@Autowired
	private SiteService siteService;
	@Autowired
	private UserService userService;
	@Autowired
	private InfoService infoService;
	@Autowired
	private InfoQueryService query;
	@Autowired
	private NodeQueryService nodeQuery;
	@Autowired
	private OperationLogService logService;
	@Autowired
	private OrgService orgService;
	@Autowired
	private AttachmentRefService attachmentRefService;
}
